package cn.edu.aust.zhuzhang.mydialogtest.View;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.Nullable;

import cn.edu.aust.zhuzhang.mydialogtest.R;

/**
 * TODO: document your custom view class.
 */
public class MyProgressBar extends View {
    private Paint mPaint;
    private int mProgress=0;                    //进度
    private int max=100;                        //最大值
    private boolean isTemp=false;               //是否支持温度显示
    private int mProgressPadding=dp2px(5);  //进度边距
    private int mHeight=0;                      //控件高度
    float mTextSize=18f;                        //字体大小
    boolean isShowText=true;                    //是否显示文字
    boolean isRadius=false;                     //进度条是否为圆角
    int mProgressColor=Color.RED;               //主进度条颜色,默认为红色
    int mProgressHeight=dp2px(10);          //主进度条高度

    int mTextColor=Color.BLUE;                  //文字颜色
    int mBackgroundColor=Color.GRAY;            //背景进度颜色
    int mBackgroundHeight=dp2px(10);        //背景进度条高度

    boolean isGrdient=false;                    //是否为渐变
    int mEndColor=Color.YELLOW;                 //渐变结束色

    public MyProgressBar(Context context) {
        this(context,null);
    }

    public MyProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);

    }

    public MyProgressBar(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        mPaint=new Paint();
        mPaint.setAntiAlias(true);  //抗锯齿
        mPaint.setColor(Color.RED);

        /*Resources.Theme theme=getContext().getTheme();
        TypedArray ta=theme
            .obtainStyledAttributes(attrs,R.styleable.MyProgressBar,0,0);*/
        TypedArray ta=getContext()
                .obtainStyledAttributes(attrs,R.styleable.MyProgressBar);

        //属性值设置
//        mHeight=(int)ta.getDimension(R.styleable.MyProgressBar_height,mHeight);
        mProgressColor=ta.getColor(R.styleable.MyProgressBar_progress_color,mProgressColor);
        mTextColor=ta.getColor(R.styleable.MyProgressBar_text_color,mTextColor);
        mBackgroundColor=ta.getColor(R.styleable.MyProgressBar_background_color,mBackgroundColor);
        mEndColor=ta.getColor(R.styleable.MyProgressBar_end_color,mEndColor);
        mProgressHeight=(int)ta.getDimension(R.styleable.MyProgressBar_progress_height,mProgressHeight);
        mBackgroundHeight=(int)ta.getDimension(R.styleable.MyProgressBar_background_height,mBackgroundColor);
        mTextSize=ta.getDimension(R.styleable.MyProgressBar_text_size,mTextSize);
        mProgress=ta.getInt(R.styleable.MyProgressBar_progress,mProgress);
        max=ta.getInt(R.styleable.MyProgressBar_maxValue,max);
        isTemp=ta.getBoolean(R.styleable.MyProgressBar_isTemp,isTemp);
        isShowText=ta.getBoolean(R.styleable.MyProgressBar_isShowText,isShowText);
        isGrdient=ta.getBoolean(R.styleable.MyProgressBar_isGradient,isGrdient);
        isRadius=ta.getBoolean(R.styleable.MyProgressBar_isRadius,isRadius);
        ta.recycle();

        mPaint.setTextSize(mTextSize);//设定文字大小
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//得到组件的大小
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode=MeasureSpec.getMode(widthMeasureSpec);//得到测量模式
        int widthVal=MeasureSpec.getSize(widthMeasureSpec);//默认用户需要给出明确值
        int height=measureHeight(heightMeasureSpec); //获得测量高度

        setMeasuredDimension(widthVal,height);
    }

    private int measureHeight(int heightMeasureSpec) {
        int result;
        int mode=MeasureSpec.getMode(heightMeasureSpec);
        int val=MeasureSpec.getSize(heightMeasureSpec);
        if (mode==MeasureSpec.EXACTLY){//用户使用精确值
            result=val;
        }else{//mode是MeasureSpec.UNSPECIFIED或MeasureSpec.AT_MOST
            //得到文字高度,descent()组件下线到基线的距离，ascent()组件上线到基线的距离
            int textHeight=(int)(mPaint.descent()-mPaint.ascent());
            result=getPaddingTop()+getPaddingBottom()+Math.max(mHeight,textHeight);
            if (mode==MeasureSpec.AT_MOST){//给定了最大值
                result=Math.min(result,val);
            }
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {//绘制组件
        super.onDraw(canvas);

        mHeight=getMeasuredHeight();
        String str=mProgress+"%";
        if (isTemp){
            str=mProgress+"℃";
        }

        float textWidth=mPaint.measureText(str);//测量获得文本长度
        if (!isShowText){
            textWidth=0;
        }

        //进度条总长度=控件宽度-文本长度-左右填充宽度-左右边距，也就是已加载的长度
        float width=getMeasuredWidth()-textWidth-getPaddingLeft()-getPaddingRight()-mProgressPadding*2;
        //根据当前进度获取进度结束位置
        float curProgress=getPaddingLeft()+width*mProgress/max;
        //设置进度条颜色
        mPaint.setColor(mProgressColor);
        //设置进度条高度
        mPaint.setStrokeWidth(mProgressHeight);

        int r=0;
        if (isRadius){//设置直线末端为圆角
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            r=mProgressHeight/2;//圆角的半径
        }

        if (isGrdient){//是否为渐变色
            //设置线性渐变，开始位置和结束位置和进度条相同
            Shader shader=new LinearGradient(getPaddingLeft(),mHeight/2,curProgress,mHeight/2,
                    mProgressColor,mEndColor, Shader.TileMode.REPEAT);
            mPaint.setShader(shader);
        }

        //画进度条，当存在圆角时，需要考虑圆角，在画直线的时候，圆角的长度是不需要考虑进去，故开始坐标+r,结束坐标-r
        //坐标原点在左上角
        canvas.drawLine(getPaddingLeft()+r,mHeight/2,curProgress-r,mHeight/2,mPaint);
        int y=(int)(-(mPaint.descent()+mPaint.ascent())/2);

        //设置未加载部分的颜色和宽度
        mPaint.setColor(mBackgroundColor);
        mPaint.setStrokeWidth(mBackgroundHeight);

        //清除渐变,并画出未加载的部分
        mPaint.setShader(null);
        canvas.drawLine(curProgress,mHeight/2,curProgress+width*(max-mProgress)/max,mHeight/2,mPaint);

        //设置文字颜色
        mPaint.setColor(mTextColor);
        if (isShowText){
            canvas.drawText(str,getMeasuredWidth()-getPaddingRight()-textWidth,mHeight/2+y,mPaint);
        }
        invalidate();//重画View
    }

    public void setProgress(int mProgress){
        this.mProgress=mProgress;
    }

    public int getProgress(){
        return mProgress;
    }

    public void setMaxValue(int max){
        this.max=max;
    }

    public int getMaxValue(){
        return this.max;
    }

    private int dp2px(int val){
        int res=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,val,getResources().getDisplayMetrics());
        return res;
    }

    private int sp2px(int val) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, val, getResources().getDisplayMetrics());
    }
}
